#include <QFileInfo>
#include <QFileDialog>
#include <QTextDocumentWriter>
#include <QFile>
#include <QByteArray>
#include <QTextCodec>

#include "calculatorwidget.h"

CalculatorWidget::CalculatorWidget(QWidget *parent):QSplitter(parent)
{
    setupEditor();
}

void CalculatorWidget::setupEditor()
{
    editor = new CalTextEdit;
    toolWidget = new CalToolWidget;
    toolWidget->setFixedWidth(150);

    addWidget(editor);
    addWidget(toolWidget);
    connect(toolWidget,SIGNAL(sendFunction(QString)),editor,SLOT(receiveFunction(QString)));
}

void CalculatorWidget::fileNew()
{
    if (maybeSave()) {
        editor->clear();
        editor->setText("---> ");
        editor->moveCursorToEnd();
        setCurrentFileName(QString());
    }
}

bool CalculatorWidget::load(const QString &f)
{
    if (!QFile::exists(f))
        return false;
    QFile file(f);
    if (!file.open(QFile::ReadOnly|QIODevice::Text))
        return false;
    QByteArray data = file.readAll();
    QTextCodec *codec = Qt::codecForHtml(data);
    QString str2 = codec->toUnicode(data);
    QString str(data);
    editor->setText(str);
    setCurrentFileName(f);
    return true;
}
void CalculatorWidget::fileOpen(QString fn)
{
    if (!fn.isEmpty())
    {
        load(fn);
    }
}

bool CalculatorWidget::fileSave()
{
    if (fileName.isEmpty())
         return fileSaveAs();

    QFile file(fileName);
    if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
        return 0;
    QTextStream stream(&file);
    QString text;
    text = editor->toPlainText();
    stream << text;
    return 1;
}

bool CalculatorWidget::fileSaveAs()
{
    QString fn = QFileDialog::getSaveFileName(this, tr("Save as..."),
                                              QString(), tr("Calculator file (*.cal)"));
    if (fn.isEmpty())
        return false;
    if (! (fn.endsWith(".cal", Qt::CaseInsensitive)))
         fn += ".cal"; // default
    setCurrentFileName(fn);
    return fileSave();
}
bool CalculatorWidget::maybeSave()
{
    if (!editor->document()->isModified())
         return true;
     if (fileName.startsWith(QLatin1String(":/")))
         return true;
     QMessageBox::StandardButton ret;
     ret = QMessageBox::warning(this, tr("Math Assistant"),
                                tr("Nội dung tập tin đã thay đổi.\n"
                                   "Bạn có muốn lưu lại không?"),
                                QMessageBox::Save | QMessageBox::Discard
                                | QMessageBox::Cancel);
     if (ret == QMessageBox::Save)
         return fileSave();
     else if (ret == QMessageBox::Cancel)
         return false;
     return true;
}

void CalculatorWidget::setCurrentFileName(const QString &fileName)
{
    this->fileName = fileName;
    editor->document()->setModified(false);

    QString shownName;
    if (fileName.isEmpty())
        shownName = "untitled.cal";
    else
        shownName = QFileInfo(fileName).fileName();

    setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("Rich Text")));
    setWindowModified(false);
}

